package com.example.mymessagequeue.mqserver.datacenter;

import com.example.mymessagequeue.MyMessageQueueApplication;
import com.example.mymessagequeue.mqserver.core.Binding;
import com.example.mymessagequeue.mqserver.core.Exchange;
import com.example.mymessagequeue.mqserver.core.ExchangeType;
import com.example.mymessagequeue.mqserver.core.MsgQueue;
import com.example.mymessagequeue.mqserver.mapper.MetaMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.List;

/**
 * 整合数据库操作
 */
public class DataBaseManager {
    // 手动拿到 metaMapper
    private MetaMapper metaMapper;

    // 数据库初始化
    public void init() {
        // 获取到 MetaMapper
        metaMapper = MyMessageQueueApplication.context.getBean(MetaMapper.class);

        if (!checkDBExists()) {
            File dataDir = new File("./data");
            dataDir.mkdirs();
            createTable();
            createDefaultData();
            System.out.println("[DataBaseManager] 初始化完成!");
        } else {
            System.out.println("[DataBaseManager] 数据库已经存在!");
        }
    }

    public void deleteDB() {
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库文件成功!");
        } else {
            System.out.println("[DataBaseManager] 删除数据库文件失败!");
        }
        File dataDir = new File("./data");
        ret = dataDir.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库目录成功!");
        } else {
            System.out.println("[DataBaseManager] 删除数据库目录失败!");
        }
    }

    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        return file.exists();
    }

    private void createTable() {
        // 不需要手动创建 meta.db
        // 首次执行这里的数据库操作时, 就会自动创建出 meta.db 文件 (mybatis 完成的)
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成!");
    }

    private void createDefaultData() {
        // 添加一个默认的交换机
        // RabbitMQ 设定, 有一个匿名的交换机, 类型是 DIRECT.
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成!");
    }

    // 把其他的数据库的操作, 也在这个类封装一下.
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MsgQueue msgQueue) {
        metaMapper.insertQueue(msgQueue);
    }

    public List<MsgQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }
}
